package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * 服务端代码
 * Created by Administrator on 2018/4/19.
 */

public class MessgerService extends Service {
    private static final String TAG = "MessengerService";
    private static class MessengerHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Log.e(TAG,"!!!!!!!!!!!!!"+msg.getData().getString("msg"));
                    Messenger client=msg.replyTo;
                    Message relpyMessage =Message.obtain(null,2);
                    Bundle bundle=new Bundle();
                    bundle.putString("reply","你的消息我已经收到，稍后回复你");
                    relpyMessage.setData(bundle);
                    try {
                        client.send(relpyMessage);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                default:
                        super.handleMessage(msg);
                        break;
            }

        }
    }
    private Messenger mMessenger = new Messenger(new MessengerHandler());

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
}
